const {
    EditorElement
} = require('./editor-element');

class Size extends EditorElement {

    static className() {
        return 'Size';
    }

    constructor(width, height) {
        super();
        this._width = width;
        this._height = height;
    }

    static isSize(size) {
        try {
            return size && size.width !== null && size.width !== undefined && size.height !== null && size.height !== undefined;
        } catch (e) {
            console.warn(e);
            return false;
        }
    }

    setupWithSize(size) {
        if (this.constructor.isSize(size)) {
            this.width = size.width;
            this.height = size.height;
        } else {
            console.log(`setupWithSize: ${size} 无效的Size对象`);
        }
    }

    get width() {
        return this._width;
    }

    set width(value) {
        this._width = value;
    }

    get height() {
        return this._height;
    }

    set height(value) {
        this._height = value;
    }

    scale(value) {
        this.width *= value;
        this.height *= value;
    }

    clone() {
        if (Size.isSize(this)) {
            return new Size(this.width, this.height);
        }
        return null;
    }
}

module.exports = {
    Size
};